package com.alibaba.sdk.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Map;

/**
 * @ author  Shawn(Shawn_0217@163.com)
 * @ date    2021/5/11 11:15
 * @ company Alibaba
 */
@Slf4j
public class HttpRequestUtil {

    /**
     * 处理请求uri
     *
     * @param request
     * @return
     */
    public static String delPathVariable(HttpServletRequest request) {
        String servletPath = request.getServletPath();
        Map pathVariable = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        if (pathVariable.isEmpty()) {
            return servletPath;
        }

        Iterator keySet = pathVariable.keySet().iterator();
        while (keySet.hasNext()) {
            String key = keySet.next().toString();
            String value = (String) pathVariable.get(key);
            servletPath = servletPath.replaceAll("/" + value, "");
        }
        return servletPath;
    }
}
